{% extends 'base.html' %}

{% block room %}
    <div class="col-xs-2 col-sm-2 col-md-2">
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <div class="page-header">
                <h3 class="text-center">
                    <img src="{{ data.portrait }}" class="img-circle" style="width: 40px; height: 40px;">
                    <a href="{% url 'userprofile' username %}" style="color: #0f0f0f">{{ username }}</a>
                </h3>
            </div>
            <div style="background-color: #bfbcbc;" class="text-center">
                <ul class="nav nav-pills nav-stacked">
                    <li class="active"><a href="#">聊天系统<span class="sr-only">(current)</span></a></li>
                    <li><a href="{% url 'back2chat' username %}" style="color: #333">聊天室</a></li>
                    <li>
                        <form method="POST" action="/logout/">
                            <input type="submit" class="btn navbar-btn" style="background-color: #bfbcbc;" value="退出登录"/>
                            <input type="hidden" name="username" value={{ username }}>
                            {% csrf_token %}
                        </form>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <div class="col-xs-7 col-md-7 col-sm-7" style="margin-top: 15px">
        <div id="room" style="height: 450px">
            <div class="panel panel-default">
                <div class="panel-heading">
                    聊天室
                </div>
                <div class="panel-body" style="height: 500px; padding: 0px; ">
                    <div class="col-md-4 col-md-offset-4" style="margin-top: 100px">
                        <h1 class="text-center">请输入房间号</h1>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="input-group">
                                    <input id="room-name-input" type="text" class="form-control" placeholder="房间号...">
                                    <span class="input-group-btn">
                                        <input id="room-name-submit" class="btn btn-default" type="button" value="进入 >>>"/>
                                    </span>
                                    <input id="username" type="hidden" value="{{ username }}"/>
                                </div><!-- /input-group -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-xs-3">
        <div class="panel panel-default text-center" style="margin-top: 15px">
           <div class="panel-heading" role="alert" >在线</div>
           <ul class="list-group">
              {% for user in online %}
                    {% if user.username == username %}
                    {% else %}
                        <li class="list-group-item">
                            <div>
                                <a href="{% url 'friendprofile' username user.username %}" class="list-group-item">
                                    <img src="{{ user.portrait }}" class="img-circle img-thumbnail" style="margin: 2px;width: 30px;height: 30px">
                                    {{ user.username }}
                                </a>
                            </div>
                        </li>
                    {% endif %}
                {% endfor %}
           </ul>
        </div>
        <div class="panel panel-default text-center">
           <div class="panel-heading" role="alert">离线</div>
           <ul class="list-group" style="margin-top: 15px">
                {% for user in offline %}
                    {% if user.username == username %}
                    {% else %}
                        <li class="list-group-item">
                            <a href="{% url 'friendprofile' username user.username %}" class="list-group-item">
                                <img src="{{ user.portrait }}" class="img-circle img-thumbnail" style="margin: 5px;width: 40px;height: 40px">
                                {{ user.username }}</a>
                        </li>
                    {% endif %}
                {% endfor %}
            </ul>
        </div>
    </div>

{% endblock room %}

{% block chatface %}
{% endblock chatface %}

{% block script %}
    <script>
        document.querySelector('#room-name-input').focus();
        document.querySelector('#room-name-input').onkeyup = function (e) {
            if (e.keyCode === 13) {  // enter松开, 触发按钮click事件
                document.querySelector('#room-name-submit').click();
            }
        };

        document.querySelector('#room-name-submit').onclick = function (e) {
            var roomName = document.querySelector('#room-name-input').value;
            var username = document.querySelector('#username').value;
            window.location.pathname = '/chat/' + roomName + '/' + username + '/';
        };
        //修改pathname，则浏览器中的地址栏url的pathname部分就会改变，并触发新的http get操作
    </script>
{% endblock script %}